Skip to content

CBv2: 1,024-token historical checkpoints, demand-targeted retention, slot-wide staging cap - #168

Merged
Gajesh2007 merged 6 commits into
mainfrom
pc/checkpoint-geometry-main
Sep 27, 2026
Merged

Gajesh2007 merged 6 commits into
mainfrom
pc/checkpoint-geometry-main

Conversation

@Gajesh2007

Copy link
Copy Markdown
Member

CBv2: 1,024-token historical checkpoints, demand-targeted retention, slot-wide staging cap

Engine half of Layr-Labs/d-inference#1207 (prefix-cache hit-rate fix set). The superproject pins this repository's main; once this merges, that PR re-pins to the new main head.

Why

Complete prefix checkpoints for the historical-attention layouts (GPT-OSS 20B, Gemma 4) were captured only where every prefill chunk below the boundary had the same size, and only two per donor (first and latest). A request that gained or lost decode company mid-prompt changed chunk size and stopped capturing, so production hits restored about 2,200 tokens of 6,700-token prompts, and a solo Gemma prompt under 2,048 tokens (46% of Gemma traffic) never checkpointed at all.

What changes

  • Capture at every 1,024-token boundary for historical layouts, whatever chunk size produced the range, including positions inside a solo stripe while the sliding ring still holds the window (recordHistorical, historicalCheckpointStrideTokens). A checkpoint at p is the owning full rows [0, p) plus each sliding owner's [p−W, p); nothing in it depends on chunk geometry. Packed rows, preemption and media still disarm capture. Recurrent (Qwen) layouts keep the uniform-chunk rule.
  • Retention: at most three per donor (CBv2HistoricalCheckpointRetention): the first boundary, the fork target floor_1024(prefixCheckpointTargetTokens) when the request carries the coordinator's observed repeated-prefix length, and the rolling latest. A target within one stride of the final latest is dropped at publication. Without a hint: first + latest.
  • Slot-wide staging cap (CBv2HistoricalStagingCap): staged windows across all donors, including those still being published, may hold at most 1/8 of slot capacity, read at the moment of use; the per-donor 1/16 budget sheds in the same order (first, then target, latest always kept). Worst case for four concurrent Gemma donors on a 16 GB slot is 1.95 GB against 2.60 GB before.
  • Historical adopters resume under ordinary chunk sizing but stay out of rectangular packed prefill (CBv2PrefixReusePlan.excludesPackedPrefill); manifests carry chunkSize = 1024 as alignment only.
  • New request field CBv2Request.prefixCheckpointTargetTokens (nil = no hint).

Before / after

flowchart LR
  classDef bad fill:#FEE2E2,stroke:#B91C1C,color:#7F1D1D
  classDef good fill:#DCFCE7,stroke:#15803D,color:#14532D
  classDef neutral fill:#F1F5F9,stroke:#475569,color:#0F172A

  subgraph B["Before"]
    direction LR
    B1["Prefill chunk sizes<br/>2048 solo → 512 with company"]:::neutral --> B2["record(): chunk size changed<br/>→ capture disarmed"]:::bad --> B3["Checkpoints: first + latest<br/>latest stuck at 2,048"]:::bad
  end
  subgraph A["After"]
    direction LR
    A1["Same chunk sizes"]:::neutral --> A2["recordHistorical(): every 1,024<br/>boundary the range covers"]:::good --> A3["Retain first, fork target, latest<br/>under a slot-wide cap"]:::good
  end
Loading

Verification

  • Engine suites on this tree: CBv2HistoricalCheckpointGeometryTests (8), CBv2HistoricalCheckpointRetentionTests (13), HistoricalWindowCheckpointEngineTests (10), CBv2PackedPrefillTests (16), CBv2CompleteCheckpointEngineTests (6), Qwen35PagedCompleteCheckpointTests (2), GPTOSSPrefillOutputTests and CBv2Gemma4ScheduledPrefillTests: 73 XCTest + 14 Swift Testing tests, 0 failures. Each behavior change has a test that fails without it (the packed-prefill guard fails three assertions with the flag off).
  • Provider (superproject) full suite on the integrated tree: 208 XCTest + 3,392 Swift Testing tests, 0 failures.
  • Live gpt-oss-20b (debug bundle): a donor that starts solo and gains company leaves checkpoints at [1024, 6144] and the repeat restores 6,144 of 6,413 tokens with sameText=true (the old rule kept 2,048); a fork prompt sharing 2,327 tokens restores the hinted 2,048 boundary; a growing conversation restores the previous turn's deepest. Live Gemma reconstruction restores 6,144 of 6,846, sameText=true.
  • Two-provider connected end-to-end gate in the superproject: nine of nine cases in both cache modes; the repeat restored 5,120 tokens where the previous record restored 4,096.
  • Independently reviewed three times in the superproject; findings (slot-wide cap, publication counted against the cap, shedding order, packed-prefill guard, anchor cap) are in these commits.

Measured sizes: gpt-oss-20b K/V is float32 on 23 of 24 layers, 49,152 B per token, 308.0 MB file at 6,144 tokens, 6.03 MB window per checkpoint; Gemma 4 window 209.7 MB per checkpoint.

🤖 Generated with Claude Code

Historical (attention-only) layouts no longer require every computed range
below a checkpoint to share one chunk cap. A checkpoint at p is the
full-attention rows [0, p) plus each sliding owner's [p-W, p), written with
absolute positions, so any stride-aligned range end is a complete model
state whatever chunk size produced it. `recordHistorical` captures at every
multiple of `historicalCheckpointStrideTokens` (1,024) a range covers; a
request that gains or loses decode company mid-prompt keeps capturing.
Packed rows, a non-contiguous range and preemption still disarm. Recurrent
layouts keep `record` and its uniform-chunk rule unchanged.

Per donor the capture stages up to K = 8 historical checkpoints (first plus
the deepest K-1, shallowest interior retired first) under a per-request
byte budget of 1/16 of the admission capacity, never below two. Measured
window reservation per checkpoint: gpt-oss-20b 4.46 MB, gemma-4-26b 216 MB.

Historical manifests carry the stride as `chunkSize`; import no longer
range-checks it against the scheduler and the adopter is not forced onto
the donor's chunk geometry (solo stripe and first-token projection apply).
Historical donors publish deepest first. `CBv2InFlightStep.historicalCheckpoints`
holds a list per request.
A 2,048-token solo stripe covers a 1,900-token prompt in one range, so a
stride-aligned checkpoint needs to land strictly inside the range. The
paged ring keeps max(window + speculative span, maxPrefillChunk) tokens
behind the highest written position (2,048 for gpt-oss and gemma-4), so
after a stripe [a, b) the sliding rows [p - W, p) for p = a + 1,024 are
still resident (gpt-oss: p - 128 >= b - 2,048; gemma-4: exactly at the
stripe's own alignment). `CBv2HistoricalWindow` now accepts a position at
or below the row frontier and keeps refusing anything older than
`oldestValidPosition`, so an evicted interior window is skipped rather
than copied stale. Full-attention pages are immutable below the frontier;
the window gathers stay in the launching step's asyncEval, before any
successor may write the ring.

Engine tests restore from interior boundaries (3c inside [2c, 4c), 5c
inside a ragged tail) and prove residency accept/refuse on the tiny ring
and at production gpt-oss/gemma-4 window geometry.
Replace the K = 8 retention with at most three checkpoints per historical
donor: the first stride boundary, the fork target the coordinator's
repeated-prefix hint names, and the rolling latest. A checkpoint is worth
its copy and its file only where a later request will share exactly that
prefix: the end of the prompt (the next turn) and a fork point (a shared
preamble, agent sub-tasks, regenerations). K = 8 wrote 2.6x the bytes of
the older first/latest rule and held the donor's terminal and KV behind
every file.

- CBv2Request.prefixCheckpointTargetTokens carries the hint (nil without
  one, 0 for a fleet-novel prompt); target = floor_stride(hint).
- CBv2HistoricalCheckpointRetention holds the rule over positions. A
  target within one stride of the final deepest boundary is dropped at
  publication. An adopter restored at M captures only above M: no first,
  and a target only above M.
- Only boundaries retention will keep are copied: the deepest capturable
  boundary of a range first, then an open first and the target below it.
- The staged byte budget reads the admission capacity at every commit.
  Over budget the target is given up; the first/latest pair is never
  split.
- Publication stays deepest first, then target, then first. Recurrent
  layouts are unchanged.

Historical adopters stopped carrying a recurrent chunk size, which was
what kept them out of rectangular packed prefill. Keep them out with an
explicit CBv2PrefixReusePlan.excludesPackedPrefill; the engine test packs
two cold rows as its control and fails without the flag.
Staged checkpoint windows and request reservations draw on one admission
ledger. A retained checkpoint serves a future request; a reservation is a
request being served now. Nothing bounded staged windows across donors, so
four concurrent gemma-4 donors could hold 3.46 GB of a 16 GB slot.

prepareHistorical now refuses a boundary (nil, the ordinary graceful path)
when every donor's staged windows plus this one's would exceed 1/8 of the
slot's admission capacity, read at that moment because a slot can be
re-sliced at runtime. The window bytes come from the allocation-free
reservation figure, so a refused boundary builds no graph and reserves
nothing.

Within one donor the claim order is rolling latest, then fork target, then
first: attempts are made in that order and a donor gives up its own first,
then its target, for a higher claim. A donor at the cap still rolls its
latest, since the replacement does not raise the total. Donors do not
displace each other. In-flight candidates are counted net of the latest
they replace and reset when the next capturing step prepares; a step
holding candidates never chains a successor.

The recurrent capture path is unchanged and has no slot-wide cap.

Tests: cap arithmetic and shedding order; N donors with the next refused,
admitted after a release, and a donor rolling at the cap; target before
first with room for one; the same reservation fitting beside capped windows
and exhausting the ledger beside uncapped ones; an engine slot that admits
a concurrent request only when the cap holds; a preempted donor releasing
its staged windows, retention state and transient charge.
The per-donor budget (1/16 of capacity) gave up the fork target and kept
the first/latest pair, while the slot-wide cap sheds the first before the
target. On an 8 GB gemma-4 slot the per-donor budget binds first, so
donors there lost the one boundary with demand evidence behind it and kept
a guessed first. Both bounds now claim in the same order: rolling latest,
then fork target, then first. Over the budget the first goes, then the
target, and the latest is always kept. Without a target (no hint, hint 0,
target not above the first, target at or beyond the latest) the pair
first + latest is kept exactly as before.

`dropInterior` is gone; the budget path uses the same `sheddable`/`shed`
the slot-wide cap uses, excluding the retained latest.
`publish` moved a donor's captures out of `staged`, but their window
reservations are released only when the publication batch closes after
the last file is written. The cap summed `staged` alone, so while a
gemma-4 donor wrote its files new donors could stage up to the full cap on
top of them: four donors finishing together beside four new ones reached
about 5.2 GB on a 16 GB slot, above the pre-change worst case.

The capture now keeps a lock-protected per-request tally of window bytes
in publication, added by `publish` on the engine queue and cleared by the
batch's close on the retirement queue once the captures have released
their reservations; `stagedHistoricalBytes` sums staged and publishing.
Recurrent captures hold no window bytes and add nothing.

The discard path (`step.discard`, preemption, a failed copy) now also
takes the candidate out of the in-flight tally through
`discardHistorical`, instead of relying on the reset at the next
capturing step.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-27T08:30:25.432991Z 3d277e9 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant